package com.yc.javaee.servlet.d0615;

import com.google.gson.Gson;
import com.yc.javaee.bean.Result;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "getLoginedUserServlet", value = "/getLoginedUser.s")
public class GetLoginedUserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        String loginedUser = (String) session.getAttribute("loginedUser");
        Gson gson = new Gson();
        Result result;
        if(loginedUser==null){
            result = new Result(0, "用户未登录", null);
        } else {
            result = new Result(1, "用户已登录", loginedUser);
        }
        String json = gson.toJson(result);
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().append(json);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
